package com.shop2cn.iapi.accounting.testcase;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;


import com.shop2cn.iapi.accounting.parameter.GetAccountBalanceListBean;
import com.shop2cn.iapi.accounting.service.GetAccountBalanceListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.shenqi.AccountCallServiceV2;
import com.ymttest.database.model.Account;
import com.ymttest.database.sqlwapper.AccountWapper;
import com.ymttest.utils.DataUtil.Random;
/**
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetAccountBalanceList{
	private static GetAccountBalanceListBean getAccountBalanceListBean;
	private static GetAccountBalanceListCall getAccountBalanceListCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量获取账户余额接口");
	}

	@Before
	public void caseUp() {
		getAccountBalanceListBean=new GetAccountBalanceListBean();
		getAccountBalanceListCall=new GetAccountBalanceListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getAccountBalanceListCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetAccountBalanceList_001() {
		Logger.start(true,"根据用户编号和账户类型获取账户余额成功");
		try {	
			Logger.comment("前置条件");
			int userId=21000015;
			int accountType=1;
			String accountid1 = AccountCallServiceV2.getAccountId(userId,accountType);
			
			AccountCallServiceV2.resetAccount(accountid1, new BigDecimal(10), new BigDecimal(20));
			Logger.comment("前置条件设置结束");
			
			List<Integer> userIdList=new ArrayList<>();
			userIdList.add(userId);
			getAccountBalanceListBean.setUserIdList(userIdList);
			getAccountBalanceListBean.setAccountType(accountType);
			getAccountBalanceListBean.setAppId("AutoTest");
			getAccountBalanceListCall.setData(getAccountBalanceListBean);
			getAccountBalanceListCall.callService();	
			Logger.verifyEquals(0, getAccountBalanceListCall.getStatus(), "验证StatusCode");
			JsonArray jal = getAccountBalanceListCall.getJsonObject().getAsJsonObject("Data").getAsJsonArray("AccountBalanceList");
			for (JsonElement jsonElement : jal) {
				JsonObject jo = jsonElement.getAsJsonObject();
				int uid=jo.get("UserId").getAsInt();
				List<Account> aids= new AccountWapper().selectAccountInfoByUserId(uid,accountType);	
				Logger.verifyEquals(0,((BigDecimal)aids.get(0).getAvailAmount()).compareTo(new BigDecimal(jo.get("AvailAmount").toString())),"验证接口返回AvailAmount:"+"跟数据库是否一致");
				Logger.verifyEquals(0,((BigDecimal)aids.get(0).getFreezeAmount()).compareTo(new BigDecimal(jo.get("FreezeAmount").toString())),"验证接口返回FreezeAmount:"+"跟数据库是否一致");			
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_GetAccountBalanceList_002() {
		Logger.start(true,"根据用户编号和账户类型批量获取账户余额成功");
		try {	
			Logger.comment("前置条件");
			int userId=21000015;
			int accountType=11;
			Map<Integer,String> uamap=new HashMap<>();
			for (int i = 0; i < 3; i++) {
				String accountid1 = AccountCallServiceV2.getAccountId(userId,accountType);
				uamap.put(userId, accountid1);
				userId++;
			}
			
			for (Integer uid : uamap.keySet()) {
				AccountCallServiceV2.resetAccount(uamap.get(uid), new BigDecimal(Random.getRandomIntFromRange(1, 10)), new BigDecimal(Random.getRandomIntFromRange(1, 10)));
			}
			
			Logger.comment("前置条件设置结束");
			
			List<Integer> userIdList=new ArrayList<>();
			for (Integer uid : uamap.keySet()) {
				userIdList.add(uid);
			}
			
			getAccountBalanceListBean.setUserIdList(userIdList);
			getAccountBalanceListBean.setAccountType(accountType);
			getAccountBalanceListBean.setAppId("AutoTest");
			getAccountBalanceListCall.setData(getAccountBalanceListBean);
			getAccountBalanceListCall.callService();	
			Logger.verifyEquals(0, getAccountBalanceListCall.getStatus(), "验证StatusCode");
			JsonArray jal = getAccountBalanceListCall.getJsonObject().getAsJsonObject("Data").getAsJsonArray("AccountBalanceList");
			for (JsonElement jsonElement : jal) {
				JsonObject jo = jsonElement.getAsJsonObject();
				int uid=jo.get("UserId").getAsInt();
				List<Account> aids= new AccountWapper().selectAccountInfoByUserId(uid,accountType);	
				Logger.verifyEquals(0,((BigDecimal)aids.get(0).getAvailAmount()).compareTo(new BigDecimal(jo.get("AvailAmount").toString())),"验证接口返回AvailAmount:"+"跟数据库是否一致");
				Logger.verifyEquals(0,((BigDecimal)aids.get(0).getFreezeAmount()).compareTo(new BigDecimal(jo.get("FreezeAmount").toString())),"验证接口返回FreezeAmount:"+"跟数据库是否一致");			
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P2.class)
	@TestCase
	@Ignore
	//case太慢手工执行
	public void Tc_GetAccountBalanceList_003() {
		Logger.start(true,"根据用户编号和账户类型批量获取50个账户余额成功");
		try {	
			Logger.comment("前置条件");
			int userId=21000015;
			int accountType=11;
			Map<Integer,String> uamap=new HashMap<>();
			for (int i = 0; i < 50; i++) {
				String accountid1 = AccountCallServiceV2.getAccountId(userId,accountType);
				uamap.put(userId, accountid1);
				userId++;
			}
			
			for (Integer uid : uamap.keySet()) {
				AccountCallServiceV2.resetAccount(uamap.get(uid), new BigDecimal(Random.getRandomIntFromRange(1, 10)), new BigDecimal(Random.getRandomIntFromRange(1, 10)));
			}
			
			Logger.comment("前置条件设置结束");
			
			List<Integer> userIdList=new ArrayList<>();
			for (Integer uid : uamap.keySet()) {
				userIdList.add(uid);
			}
			
			getAccountBalanceListBean.setUserIdList(userIdList);
			getAccountBalanceListBean.setAccountType(accountType);
			getAccountBalanceListBean.setAppId("AutoTest");
			getAccountBalanceListCall.setData(getAccountBalanceListBean);
			getAccountBalanceListCall.callService();	
			Logger.verifyEquals(0, getAccountBalanceListCall.getStatus(), "验证StatusCode");
			JsonArray jal = getAccountBalanceListCall.getJsonObject().getAsJsonObject("Data").getAsJsonArray("AccountBalanceList");
			for (JsonElement jsonElement : jal) {
				JsonObject jo = jsonElement.getAsJsonObject();
				int uid=jo.get("UserId").getAsInt();
				List<Account> aids= new AccountWapper().selectAccountInfoByUserId(uid,accountType);	
				Logger.verifyEquals(0,((BigDecimal)aids.get(0).getAvailAmount()).compareTo(new BigDecimal(jo.get("AvailAmount").toString())),"验证接口返回AvailAmount:"+"跟数据库是否一致");
				Logger.verifyEquals(0,((BigDecimal)aids.get(0).getFreezeAmount()).compareTo(new BigDecimal(jo.get("FreezeAmount").toString())),"验证接口返回FreezeAmount:"+"跟数据库是否一致");			
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P2.class)
	@TestCase
	public void Tc_GetAccountBalanceList_004() {
		Logger.start(true,"根据用户编号和账户类型批量获取51个账户余额失败");
		try {	
			Logger.comment("前置条件");
			int userId=21000015;
			int accountType=11;
			Map<Integer,String> uamap=new HashMap<>();
			for (int i = 0; i < 51; i++) {
//				String accountid1 = AccountCallServiceV2.getAccountId(userId,accountType);
				uamap.put(userId, "");
				userId++;
			}
			
//			for (Integer uid : uamap.keySet()) {
//				AccountCallServiceV2.resetAccount(uamap.get(uid), new BigDecimal(Random.getRandomIntFromRange(1, 10)), new BigDecimal(Random.getRandomIntFromRange(1, 10)));
//			}
//			
			Logger.comment("前置条件设置结束");
			
			List<Integer> userIdList=new ArrayList<>();
			for (Integer uid : uamap.keySet()) {
				userIdList.add(uid);
			}
			
			getAccountBalanceListBean.setUserIdList(userIdList);
			getAccountBalanceListBean.setAccountType(accountType);
			getAccountBalanceListBean.setAppId("AutoTest");
			getAccountBalanceListCall.setData(getAccountBalanceListBean);
			getAccountBalanceListCall.callService();	
			Logger.verifyEquals(2, getAccountBalanceListCall.getStatus(), "验证StatusCode");
			Logger.verifyEquals("一次最多查询50个用户！", getAccountBalanceListCall.getString("Message"), "验证Message");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetAccountBalanceList_005() {
		Logger.start(true,"accountType传错时不会获取到数据");
		try {	
			Logger.comment("前置条件");
			int userId=11000015;
			int accountType=1;
			String accountid1 = AccountCallServiceV2.getAccountId(userId,accountType);
			
			AccountCallServiceV2.resetAccount(accountid1, new BigDecimal(10), new BigDecimal(20));
			Logger.comment("前置条件设置结束");
			
			List<Integer> userIdList=new ArrayList<>();
			userIdList.add(userId);
			getAccountBalanceListBean.setUserIdList(userIdList);
			getAccountBalanceListBean.setAccountType(11);
			getAccountBalanceListBean.setAppId("AutoTest");
			getAccountBalanceListCall.setData(getAccountBalanceListBean);
			getAccountBalanceListCall.callService();	
			Logger.verifyEquals(0, getAccountBalanceListCall.getStatus(), "验证StatusCode");
			JsonArray jal = getAccountBalanceListCall.getJsonObject().getAsJsonObject("Data").getAsJsonArray("AccountBalanceList");
			Logger.verifyEquals(0, jal.size() ,"验证size(没有获取到列表)");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetAccountBalanceList_006() {
		Logger.start(true,"不输入参数userIdList,无返回");
		try {	
			Logger.comment("前置条件");
			int accountType=1;
//			String accountid1 = AccountCallServiceV2.getAccountId(userId,accountType);
//			
//			AccountCallServiceV2.resetAccount(accountid1, new BigDecimal(10), new BigDecimal(20));
			Logger.comment("前置条件设置结束");
			
//			userIdList.add(userId);
//			GetAccountBalanceListBean.setUserIdList(userIdList);
			getAccountBalanceListBean.setAccountType(accountType);
			getAccountBalanceListBean.setAppId("AutoTest");
			getAccountBalanceListCall.setData(getAccountBalanceListBean);
			getAccountBalanceListCall.callService();	
			Logger.verifyEquals(0, getAccountBalanceListCall.getStatus(), "验证StatusCode");
			JsonArray jal = getAccountBalanceListCall.getJsonObject().getAsJsonObject("Data").getAsJsonArray("AccountBalanceList");
			Logger.verifyEquals(0, jal.size() ,"验证size(没有获取到列表)");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetAccountBalanceList_007() {
		Logger.start(true,"不输入参数AccountType,默认1普通人民币账户");
		try {	
			Logger.comment("前置条件");
			int userId=21000015;
			int accountType=1;
			String accountid1 = AccountCallServiceV2.getAccountId(userId,accountType);
//			
			AccountCallServiceV2.resetAccount(accountid1, new BigDecimal(10), new BigDecimal(20));
			Logger.comment("前置条件设置结束");
			
			List<Integer> userIdList=new ArrayList<>();
			userIdList.add(userId);
			getAccountBalanceListBean.setUserIdList(userIdList);
//			GetAccountBalanceListBean.setAccountType(accountType);
			getAccountBalanceListBean.setAppId("AutoTest");
			getAccountBalanceListCall.setData(getAccountBalanceListBean);
			getAccountBalanceListCall.callService();	
			Logger.verifyEquals(0, getAccountBalanceListCall.getStatus(), "验证StatusCode");
			JsonArray jal = getAccountBalanceListCall.getJsonObject().getAsJsonObject("Data").getAsJsonArray("AccountBalanceList");
			for (JsonElement jsonElement : jal) {
				JsonObject jo = jsonElement.getAsJsonObject();
				int uid=jo.get("UserId").getAsInt();
				List<Account> aids= new AccountWapper().selectAccountInfoByUserId(uid,accountType);	
				Logger.verifyEquals(0,((BigDecimal)aids.get(0).getAvailAmount()).compareTo(new BigDecimal(jo.get("AvailAmount").toString())),"验证接口返回AvailAmount:"+"跟数据库是否一致");
				Logger.verifyEquals(0,((BigDecimal)aids.get(0).getFreezeAmount()).compareTo(new BigDecimal(jo.get("FreezeAmount").toString())),"验证接口返回FreezeAmount:"+"跟数据库是否一致");			
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetAccountBalanceList_008() {
		Logger.start(true,"不输入参数appid,失败");
		try {	
			Logger.comment("前置条件");
			int userId=21000015;
			int accountType=1;
			String accountid1 = AccountCallServiceV2.getAccountId(userId,accountType);
//			
			AccountCallServiceV2.resetAccount(accountid1, new BigDecimal(10), new BigDecimal(20));
			Logger.comment("前置条件设置结束");
			
			List<Integer> userIdList=new ArrayList<>();
			userIdList.add(userId);
			getAccountBalanceListBean.setUserIdList(userIdList);
			getAccountBalanceListBean.setAccountType(accountType);
//			GetAccountBalanceListBean.setAppId("AutoTest");
			getAccountBalanceListCall.setData(getAccountBalanceListBean);
			getAccountBalanceListCall.callService();	
			Logger.verifyEquals(2, getAccountBalanceListCall.getStatus(), "验证StatusCode");
			Logger.verifyEquals("AppId不能为空！", getAccountBalanceListCall.getString("Message"), "验证Message");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}